/*
* created by Administrator on 2018/11/30
* 好的代码像粥一样,是得用时间和精力熬的。
*/
let generator = function * (fn) {
    try{
        let res1 = yield delay(4000,4);  //stop
        fn(res1);
        let res2 = yield delay(4000,4);  //stop
        fn(res2);
        let res3 = yield delay(4000,4);  //stop
        fn(res3);
        let res4 = yield delay(4000,4);  //stop
        fn(res4);
    }catch (e){
        console.log(e);
    }

};
// let it = generator(function (data){
//     console.log(data);
// });
function delay(time,value){
    // setTimeout(function(){
    //     it.next(value);
    // },time)
    return new Promise(function(resolve,reject) {
        setTimeout(function () {
            resolve(value);
        }, time)
    })
}
//it.next(); //
//generator 函数
//yield
// let generator = function * () {
//    let res = yield 1;
//     console.log(res);
//     yield 2;
//     yield 3;
//     return 4;
// };
// let a = generator();
// a.next(2134235);

// let generator = function * () {
//     let a = yield 1;
//     let b =yield 2;
//     let c =yield 3;
//     console.log(a+b+c);
// };
// let a = generator();
// a.next(100);
// a.next(200);

//async 异步函数 await【等待】==>>promise
//async 函数会返回promise
//async 函数不会阻塞主线程
    let d =async function(){
         let a = delay(1000,5).then((data)=>console.log(data)).catch();
         let b = delay(2000,6).then((data)=>console.log(data)).catch();
         let c = delay(3000,7).then((data)=>console.log(data)).catch();
        console.log(123);
         let f = delay(4000,8).then((data)=>console.log(data)).catch();
         await a;
         await b;
         await c;
         await f;
    };
    // d().then(function(data){console.log(data)});
    d(function(data){
    console.log(data);
    });10.10.25.174

//串行：你跑完我接着跑
//并行：一起跑

